library(sf)
library(tidyverse)
library(ragg)
# this code was written before the release of sf
sf_use_s2(FALSE)

africa_grid_covariates <- st_read("Data/ML Prediction/Predictors/gridded_covariates_snl.gpkg")

agg_png("./Figures/Figure-A-1-Common-Support.png", 
        width = 14, height = 14, res = 700, units = "in")
print(
africa_grid_covariates %>% 
  group_by(gid) %>% 
  mutate(missing_ERA = is.na(ERA_Arc), 
         missing_glg = is.na(GLG_K), 
         missing_sub = is.na(SUBauid), 
         missing_mag = is.na(mag)) %>% 
  ungroup() %>%
  select(gid, starts_with("missing")) %>% 
  pivot_longer( cols = starts_with("missing"), values_to = "Missingness") %>% 
  mutate(name = case_when(name == "missing_ERA" ~ "Missing Age of the Bedrock", 
                          name == "missing_glg" ~ "Missing Geological Type", 
                          name == "missing_mag" ~ "Missing Electromagnatism", 
                          name == "missing_sub" ~ "Missing Geological Subtype")) %>%
  {ggplot() + 
      geom_sf(aes(fill = Missingness), color = NA, size = .01, data = .) + 
      facet_wrap(~name) + 
      scale_fill_brewer(palette = "Set1", direction = -1) +
      theme(text = element_text(size=20))+ 
      labs(title = "Missingness of Geological Predictors")}
)
dev.off()
